package com.spring.demo8.eg9;

import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @author vhans
 */
public class PostProcessAfterInitializationTest {
    @Test
    public void test() {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

        // 加入bean初始化后置处理器方法实现
        factory.addBeanPostProcessor(new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                System.out.println("postProcessAfterInitialization: " + beanName);
                return bean;
            }
        });

        factory.registerBeanDefinition("name", BeanDefinitionBuilder.genericBeanDefinition(String.class)
                .addConstructorArgValue("Vhans")
                .getBeanDefinition());

        factory.registerBeanDefinition("personInformation", BeanDefinitionBuilder.genericBeanDefinition(String.class)
                .addConstructorArgValue("学习Spring")
                .getBeanDefinition());

        System.out.println("======输出bean信息======");
        for (String beanName : factory.getBeanDefinitionNames()) {
            System.out.println(beanName + "->" + factory.getBean(beanName));
        }
    }
}
